package com.ruoyi.project.wechatprogram.picture.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.project.wechatprogram.menu.controller.WxMenuController;
import com.ruoyi.project.wechatprogram.menu.domain.WxMenu;
import com.ruoyi.project.wechatprogram.weekend.domain.WxWeekend;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.wechatprogram.picture.domain.WxPicture;
import com.ruoyi.project.wechatprogram.picture.service.IWxPictureService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 轮播图Controller
 * 
 * @author sxchen
 * @date 2020-05-06
 */
@Controller
@RequestMapping("/wechatprogram/picture")
public class WxPictureController extends BaseController
{

    private static final Logger log = LoggerFactory.getLogger(WxPictureController.class);

    private String prefix = "wechatprogram/picture";

    @Autowired
    private IWxPictureService wxPictureService;

    @RequiresPermissions("wechatprogram:picture:view")
    @GetMapping()
    public String picture()
    {
        return prefix + "/picture";
    }

    /**
     * 查询轮播图列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request)
    {
        startPage();
        List<WxPicture> pictureList = wxPictureService.selectWxPictureList(null);
        //获取服务器域名
        String serverUrl = ServerConfig.getDomain(request);
        for (WxPicture picture : pictureList) {
            picture.setPicUrl(serverUrl + picture.getPicUrl());
        }
        return getDataTable(pictureList);
    }

    /**
     * 导出轮播图列表
     */
    @RequiresPermissions("wechatprogram:picture:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WxPicture wxPicture)
    {
        List<WxPicture> list = wxPictureService.selectWxPictureList(wxPicture);
        ExcelUtil<WxPicture> util = new ExcelUtil<WxPicture>(WxPicture.class);
        return util.exportExcel(list, "picture");
    }

    /**
     * 新增轮播图
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存轮播图
     */
    @RequiresPermissions("wechatprogram:picture:add")
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, @RequestParam("picMessage")String picMessage) {
        WxPicture wxPicture = new WxPicture();
        try
        {
            if (!file.isEmpty())
            {
                String pictureUrl = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
                wxPicture.setPicMessage(picMessage);
                wxPicture.setPicUrl(pictureUrl);
                wxPicture.setCreateTime(new Date());
                if (wxPictureService.insertWxPicture(wxPicture) > 0)
                {
                    return success();
                }
            }
            return error();
        }
        catch (Exception e)
        {
            log.error("修改轮播图失败！", e);
            return error(e.getMessage());
        }
    }

    /**
     * 修改轮播图
     */
    @GetMapping("/edit/{picId}")
    public String edit(@PathVariable("picId") Integer picId, ModelMap mmap)
    {
        WxPicture wxPicture = wxPictureService.selectWxPictureById(picId);
        mmap.put("wxPicture", wxPicture);
        return prefix + "/edit";
    }

    /**
     * 修改保存轮播图
     */
    @RequiresPermissions("wechatprogram:picture:edit")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam("picId")Integer picId, @RequestParam("file")MultipartFile file,@RequestParam("picMessage")String picMessage) {
        WxPicture wxPicture = new WxPicture();
        try
        {
            if (!file.isEmpty())
            {
                String pictureUrl = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
                wxPicture.setPicId(picId);
                wxPicture.setPicUrl(pictureUrl);
                wxPicture.setPicMessage(picMessage);
                wxPicture.setUpdateTime(new Date());
                if (wxPictureService.updateWxPicture(wxPicture) > 0)
                {
                    return success();
                }
            }
            return error();
        }
        catch (Exception e)
        {
            log.error("修改轮播图失败！", e);
            return error(e.getMessage());
        }
    }

    /**
     * 删除轮播图
     */
    @RequiresPermissions("wechatprogram:picture:remove")
    @Log(title = "轮播图", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wxPictureService.deleteWxPictureByIds(ids));
    }
}
